package com.coolingme.hutool;

import cn.hutool.aop.aspects.SimpleAspect;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.lang.Console;

import java.lang.reflect.Method;

/**
 * 匹配人的切面
 *
 * @author wangyue
 * @date 2022/8/9 20:31
 */
public class MatchAspect extends SimpleAspect {

    /**
     * TimeInterval为Hutool实现的一个计时器
     */
    private TimeInterval interval = new TimeInterval();

    /**
     * Cglib代理：cn.hutool.aop.interceptor.CglibInterceptor#intercept(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], net.sf.cglib.proxy.MethodProxy)
     */

    @Override
    public boolean before(Object target, Method method, Object[] args) {
        interval.start();
        System.out.println("--------------------------------------");
        System.out.println("媒婆开始咨询");
        return true;
    }

    @Override
    public boolean after(Object target, Method method, Object[] args, Object returnVal) {
        Console.log("Method [{}.{} ({})] execute spend [{}]ms", target.getClass().getName(), method.getName(), args, interval.intervalMs());
        System.out.println(target.getClass().getName());
        if (target instanceof PoolMan) {
            System.out.println("媒婆：爪巴!");
        } else if (target instanceof RichMan) {
            System.out.println("媒婆：彳亍!");
        } else {
            System.out.println("媒婆：安排!");
        }
        return true;
    }

    @Override
    public boolean afterException(Object target, Method method, Object[] args, Throwable e) {
        // 无论target.method是否抛出异常，均会执行after()
        return false;
    }
}
